package com.czy.utilslib.file;


import android.content.Context;
import android.content.SharedPreferences;
import android.security.keystore.KeyGenParameterSpec;

import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKeys;

import com.czy.utilslib.BaseApplication;

public class SecuritySharedPreferencesUtils {

    public static SharedPreferences getSecuritySharedPreferences(String fileName){
        assert fileName != null && !fileName.isEmpty();
        try {
            KeyGenParameterSpec keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC;
            String masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec);
            return EncryptedSharedPreferences.create(
                    fileName,
                    masterKeyAlias,
                    BaseApplication.getInstance(),
                    EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                    EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
            );
        } catch (Exception e) {
            e.printStackTrace();
            return BaseApplication.getInstance().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        }
    }

}
